package ink.sunflowerk.config;


import com.fasterxml.jackson.core.JsonFactory;
import com.google.gson.Gson;
import ink.sunflowerk.exception.AccessForbiddenException;
import ink.sunflowerk.exception.TeacherLoginException;
import ink.sunflowerk.util.CrowdUtil;
import ink.sunflowerk.util.ResultEntity;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zhaokuii11@163.com
 * @create 2021-12-01 21:14
 * @Description
 */
//@ControllerAdvice 表示当前类是一个基于注解的异常处理类
@ControllerAdvice
public class CrowdExceptionResolver {

    @ExceptionHandler(value = TeacherLoginException.class)
    public ModelAndView resolveLoginFailedException(
            //实际捕获的德类型
            TeacherLoginException exception,
            //当前请求对象
            HttpServletRequest request,
            //当前响应对象
            HttpServletResponse response) throws IOException {
        return commonResolve("login", exception, request, response);
    }

    @ExceptionHandler(value = AccessForbiddenException.class)
    public ModelAndView accessForbiddenException(
            //实际捕获的德类型
            AccessForbiddenException exception,
            //当前请求对象
            HttpServletRequest request,
            //当前响应对象
            HttpServletResponse response) throws IOException {
        return commonResolve("login", exception, request, response);
    }


    /**
     * 抽取公共代码
     *
     * @param viewName  发生异常要去的页面
     * @param exception 发生的异常
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    protected ModelAndView commonResolve(
            //要去的视图
            String viewName,
            //实际捕获的德类型
            Exception exception,
            //当前请求对象
            HttpServletRequest request,
            //当前响应对象
            HttpServletResponse response) throws IOException {

        //1. 判断当前请去
        boolean requestType = CrowdUtil.judgeRequestType(request);
        //2. 如果 Ajax请求
        if (requestType) {
            //3. 创建 ResultEntity 对象
            ResultEntity<Object> resultEntity = ResultEntity.failed(exception.getMessage());
            //4. 消息转为 json对象，创建 gson对象
            JSONObject jsonObject = new JSONObject();
            //Gson gson = new Gson();
            //5. 将 json字符串作为响应体返回给浏览器
            jsonObject.opt(resultEntity.toString());
            //String json = gson.toJson(resultEntity);
            //6. 将 json字符串作为响应体返回给浏览器
            //response.getWriter().print(json);
            response.getWriter().print(jsonObject);
            //7. 由于上面通过原生的 response对象返回了响应，所以不提供 modelAndView对象
            return null;
        }
        //8. 如果不是 Ajax请去则创建 ModelAndView对象
        ModelAndView view = new ModelAndView();
        //9. 将 exception对象存入模型
        view.addObject("exception", exception);
        //10. 设置对应的视图
        view.setViewName(viewName);
        //11. 返回
        return view;
    }


}
